import Request from "@/api/request";
import store from "@/store";
// in .ts file use this router; in .vue use useRouter()
import router from "@/router/index.ts";
import { User } from "@/interface/entity";
import { generateUserList, processUserBeforeToDb } from "@/functions/user";
import { constObjs } from "@/const/const";
import { ElMessage } from "element-plus";
import { querySystemSetting } from "./SystemSettingApi";
import { FormPatternItem } from "@/interface/general";

export async function userLogin(username: string, password: string) {
  await Request({
    method: "get",
    url: "User/GetToken",
    params: {
      username: username,
      password: password
    }
  })
    .then(async response => {
      // save token
      localStorage.setItem("token", response.data);
      // save username and user type
      store.state.username = username;
      // 获取当前用户信息
      await Request({
        method: "get",
        url: "User/QueryNowUser",
        params: {
          username: username
        }
      }).then(response => {
        store.state.nowUser = response.data as User;
        store.state.nowUserPermissionList = store.state.nowUser.permissionString.split(
          ","
        );
      });
      // 获取tablepattern
      await querySystemSetting("123");
      router.push({ name: "index" });
      ElMessage.success("登陆成功");
    })
    .catch(() => {
      router.push({ name: "login" });
    });
}
// logout
export function logout() {
  localStorage.clear();
  store.state.username = "";
  store.state.projectList = [];
  store.state.tableData = [];
  // goback to login page
  router.push({ name: "login" });
}

export const queryUserNameList = async () => {
  await Request({
    method: "get",
    url: "User/QueryUserNameList"
  }).then(response => {
    const userNameList = response.data as string[];
    const patternItem0 = constObjs.projectFormPattern
      .formItem[0] as FormPatternItem[];
    patternItem0[1].options = [];
    userNameList.forEach(name => {
      patternItem0[1].options?.push({
        name: name,
        value: name
      });
    });
  });
};

export const tryToIncomeStreamSystem = async () => {
  await Request({
    method: "get",
    url: "User/ToIncomeStreamSystem"
  });
};
export const tryToProjectSystem = async () => {
  await Request({
    method: "get",
    url: "User/ToProjectSystem"
  });
};
export const tryToOutlayStreamSystem = async () => {
  await Request({
    method: "get",
    url: "User/ToOutlayStreamSystem"
  });
};

// export const queryNowUser = (username: string) => {};

// -----------------admin only-----------------------
export const tryToAdminSystem = async () => {
  await Request({
    method: "get",
    url: "User/ToAdminSystem"
  });
};
export const addUserToDb = async (user: User) => {
  user = processUserBeforeToDb(user);
  await Request({
    method: "post",
    url: "User/AddUserByAdmin",
    params: {
      username: user.username,
      password: user.password,
      permissionString: user.permissionString
    }
  });
};

export const deleteUserFromDb = async (userID: number) => {
  Request({
    method: "delete",
    url: "User/DeleteUserByAdmin",
    params: {
      userID: userID
    }
  });
};

// update
export const updateUserToDb = async (user: User) => {
  user = processUserBeforeToDb(user);
  Request({
    method: "put",
    url: "User/UpdateUserByAdmin",
    params: {
      username: user.username,
      password: user.password,
      permissionString: user.permissionString,
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
      userID: user.userID!
    }
  });
};

//query user
export async function queryAllUser() {
  Request({
    method: "get",
    url: "User/QueryAllUserByAdmin"
  }).then(response => {
    store.state.tableData = generateUserList(response.data as User[]);
  });
}
